// index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 嵌套数据 [obj,obj,obj] ; obj={id:xx,name:xx,children:[obj,obj]}
    items: {type: Array, value: []},
    // 主键字段名
    "id_key": {type: String, value: "id"},
    // 显示字段名
    "name_key": {type: String, value: "name"},
    // 子列表字段名
    "children_key": {type: String, value: "children"},
    //最大层数
    "max_level": {type: Number, value: 5},
    // 选中的最后一层obj.id, 以渲染picker的index
    value: {
      type: Number, value: null,
      observer: function(newData, oldData) {
        if(newData!==oldData) {
          this._resetPickerRange();
          this._fireChangeEvent(false);
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    //picker's range property
    range: [],
    //每列选中的index
    indexValues: [],
  },

  lifetimes: {
    attached: function() {
    },
    detached: function() {
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _bindchange: function(event) {
      let component = this;
      this.setData({originRange: Array.from(this.data.range), originIndexValues: Array.from(this.data.indexValues)});
      this._fireChangeEvent(true);
    },
    _bindcancel: function (event) {
      this.setData({range: Array.from(this.data.originRange), indexValues: Array.from(this.data.originIndexValues)});
    },
    _bindcolumnchange: function(event) {
      let component = this;
      let column = event.detail.column, indexValue = event.detail.value;
      let list = this.data.range[column];
      // 修改indexValues值， 重新规划range
      let indexValues = this.data.indexValues, range = this.data.range;
      indexValues[column] = indexValue;
      //清除column之后的indexValues, range， 对range[column].children递归出后续的range和index(默认0)
      range.splice(column+1, 99);
      indexValues.splice(column+1, 99);
      function recursiveSetZero(list, _currentLevel) {
        if(_currentLevel > component.data.max_level) {
          return;
        }
        indexValues.push(0);
        range.push(list);
        if(list[0] && list[0][component.data.children_key] && list[0][component.data.children_key].length>0) {
          recursiveSetZero(list[0][component.data.children_key], _currentLevel+1);
        }
      }
      if(range[column][indexValue][component.data.children_key] && range[column][indexValue][component.data.children_key].length>0) {
        recursiveSetZero(range[column][indexValue][component.data.children_key], indexValues.length+1);
      }
      component.setData({range,indexValues});
    },
    //
    /**
     * 根据data.value的值整理picker的range 和 index value
     * @private
     */
    _resetPickerRange: function () {
      if(this.data.items.length===0) {
        return;
      }
      let component = this;
      let indexValues = [], range = [];
      if(component.data.value) {
        //从value向上追溯, 整理indexValues
        let tempIndexValues = [], tempRange = [];
        function recursiveRead(list, currentLevel) {
          if(currentLevel > component.data.max_level) {
            //如果当前层数大于最大层数，返回
            return false;
          }
          for(let index=0;index<list.length;index++) {
            let item = list[index];
            let children = item[component.data.children_key];
            if (children && children.length > 0) {
              let ret = recursiveRead(children, currentLevel+1);
              if (ret) {
                tempIndexValues.push(index);
                tempRange.push(children);
                return true;
              }
            } else if (item[component.data.id_key] == component.data.value) {
              tempIndexValues.push(index);
              return true;
            }
          }
          return false;
        }
        recursiveRead(this.data.items, 1);
        if(tempIndexValues.length>0) {
          indexValues = tempIndexValues.reverse();
          tempRange.push(this.data.items);
          range = tempRange.reverse();
        }
      }
      if(indexValues.length===0) {
        //全部默认选中index 0
        function recursiveSetZero(list, currentLevel) {
          if(currentLevel > component.data.max_level) {
            //如果当前层数大于最大层数，返回
            return false;
          }
          indexValues.push(0);
          range.push(list);
          if(list[0] && list[0][component.data.children_key] && list[0][component.data.children_key].length>0) {
            recursiveSetZero(list[0][component.data.children_key], currentLevel+1);
          }
        }
        recursiveSetZero(this.data.items, 1);
      }
      this.setData({range, indexValues, originRange:Array.from(range), originIndexValues: Array.from(indexValues)});
    },
    _fireChangeEvent: function(isManual) {
      let component = this;
      let items = [], indexValues = [];
      if(this.data.indexValues.length>0) {
        this.data.indexValues.forEach(function (indexValue, index) {
          items.push(component.data.range[index][indexValue]);
          indexValues.push(indexValue);
        });
        this.triggerEvent('change', {items, indexValues, isManual: isManual});
      }
    }
  },

  observers: {
    'items': function(items) {
      this._resetPickerRange();
      if(this.data.value) {
        this._fireChangeEvent(false);
      }
    },
  }
});
